#ifndef NODE_H
#define NODE_H

#include <QList>
#include <QString>
#include <QVariant>

class QPainter;
class QPixmap;
class Scene;

class Node
{
private:
	static Scene* scene;
	Node* m_pParent;
	QList<Node*> children;
	bool m_Selected;

	Node& operator=(const Node&);
	friend class Scene;
public:
	Node();
	virtual ~Node(void);
	
	QHash<QString, QVariant> attr;

	enum NodeType { NNode = 0, NSprite, NLayer };

	//hierarchy
	Node* parent() const { return m_pParent; }
	int row() const {
		if (m_pParent) return m_pParent->children.indexOf(const_cast<Node*>(this));

		return 0;
	}
	int childCount() const {
		return children.count();
	}
	Node* child(int index) const {
		return children.value(index);
	}
	const QList<Node*> getChildren() const { return children; }
	void addChild(Node* node);
	void addChildAt(Node* node, int pos);
	bool removeChild(Node* node);
	void detach() {
		if (m_pParent) m_pParent->removeChild(this);
	}

	//property
private:
	float m_X, m_Y;
	float m_ScaleX, m_ScaleY;
public:
	QString name;
	QString typeName() const;
	float x() const { return m_X; }
	float y() const { return m_Y; }
	float scaleX() const { return m_ScaleX; }
	float scaleY() const { return m_ScaleY; }
	void setPos(float x, float y);
	void setX(float x);
	void setY(float y);
	void setScaleX(float sx);
	void setScaleY(float sy);

	virtual QTransform transform() const;
	virtual QTransform worldToNodeTransform() const;
	//state
	bool selected() { return m_Selected; }
	void setSelected(bool sel);
	
	//strange


	//override
	virtual NodeType type() const { return NNode; }
	virtual void paint(QPainter& painter);
	virtual bool contains(const QPointF& point) const;
	virtual QRectF boundingRect() const;
};

class SpriteNode
	: public Node
{
public:
	SpriteNode();

	//override
	NodeType type() const { return NSprite; }
	virtual void paint(QPainter& painter);
	virtual bool contains(const QPointF& point) const;
	virtual QRectF boundingRect() const;

	static QPixmap& defaultImage();
};

class LayerNode
	: public Node
{                                                                                                                                                                  
public:
	LayerNode();
	//override
	NodeType type() const { return NLayer; }
	virtual void paint(QPainter& painter);
};

Q_DECLARE_METATYPE(Node*)

#endif